import { Behaviour, HitTestScript, TextRenderer, GameObject,getGameObjectById,core } from "./gameengine";
import { user } from "./user";
import {bear}from "./bear"
import{victory,defeated}from "./data"
import{RectCollider}from "./rect/RectCollider"
import { ColliderSystem } from "./rect/ColliderSystem";


export class Fight extends Behaviour {
    startbattle=false;
    infight=true;
    bleed=bear.att-user.def;
    battleresault=0;
    rst:GameObject;


    // private speedX = 0;
    // private speedY = 0;


    protected onStart() {
        


        

    }

    protected onUpdate(advancedTime: number) {

        const textRender1 = this.displayObject.children[0].getScript(TextRenderer);
        textRender1.text = user.hp.toString();
        const textRender2 = this.displayObject.children[2].getScript(TextRenderer);
        textRender2.text = "VS";
        const textRender3 = this.displayObject.children[3].getScript(TextRenderer);
        textRender3.text = user.att.toString();
        const textRender4 = this.displayObject.children[5].getScript(TextRenderer);
        textRender4.text = "START!";
        const textRender5 = this.displayObject.children[1].getScript(TextRenderer);
        textRender5.text = bear.hp.toString();
        const textRender6 = this.displayObject.children[4].getScript(TextRenderer);
        textRender6.text = bear.att.toString();
        
        

        

        if(!this.startbattle){
            textRender4.displayObject.getScript(HitTestScript).onClick = () => {
            console.log("start battle!");
             this.startbattle=true;
            }
        } 
        if(this.startbattle){

            if(this.infight){
                    user.hp-=this.bleed;
                    bear.hp-=user.att;
                    if(user.hp>0 && bear.hp>0){
                        this.infight=true;
                    }else{
                        this.infight=false;
                    }
                    if(!this.infight){
                        if(user.hp>=0){
                            this.battleresault=1;
                        }else{
                            this.battleresault=2;
                        }

                    }
                }
        }
        if(this.battleresault==1){
            this.displayObject.parent.removeChild(this.displayObject);
            this.displayObject = null;
            this.rst = core.createPrefab(victory);
                const scene = getGameObjectById("UIScene");
                scene.addChild(this.rst);
                bear.hp=300;
               window.addEventListener('click',(ev)=>{
        if(true){
            if (this.rst) {
                //this.rst.parent.children[2].removeAllChildren;
                //this.rst.parent.removeChild(this.rst.parent.children[2]);
                
                console.log(this.rst.parent.children[2])
                  this.rst.parent.removeChild(this.rst)
                  this.rst = null;
                  
            }
            
            ev.preventDefault();
        }

        })
            }

        
        if(this.battleresault==2){
            this.displayObject.parent.removeChild(this.displayObject)
            this.displayObject = null;
            this.rst = core.createPrefab(defeated);
                const scene = getGameObjectById("UIScene");
                scene.addChild(this.rst);

        }

    }
}